/**
 *  生成Generator与迭代器Iterator
 *  
 *  */


function read(books){
    let index = 0
    return {
        next(){
            let done = index == books.length
            let value = done ? undefined : books[index++]
            return {
                value,
                done
            }
        }
    }
}

// 迭代器
let it = read(['js', 'node'])
// it有一个方法next，每次调用next都会返回一个结果{value,done}

// let result = {}
// do{
//     result = it.next()
//     console.log(result)
// }while(!result.done)



// 生成器generator函数, 返回一个迭代器
// 执行可以暂停

function *read1(books){
    console.log("开始")
    for(let i=0; i<books.length; i++){
        // yield 放弃 屈服 产出
        yield books[i]
    }
    console.log("结束")
}
let it1 = read1(['js', 'node', 'webpack'])
let r1 = it1.next()
let r2 = it1.next()
let r3 = it1.next()
it1.next()
console.log(r3)


var books = new Map()
books.set('js', {name: 'js'})









